﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using One.Web.UI.WebControls;
using Resources;
using One.Web;
using One.Framework.Security;
using One.Framework;
using Money.Interfaces;
using Money.Data;

public partial class Admin_EditMyInfo : System.Web.UI.Page
{
    #region fields
    // xem xet co nen giu page hien tai de tra ve cho paging ko
    private IUserService userService;
    private IRoleService roleService;
    private List<Role> roles;
    protected int userId;
    protected User user = null;
    private string redirectUrl = "AccountManagement.aspx";

    #endregion

    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        userService = ServiceFactory.Create<IUserService>();
        roleService = ServiceFactory.Create<IRoleService>();

        GetParams();
        LoadData();
        AddRules();
        if (!IsPostBack)
        {
            UpdateView();
        }
    }

    protected void AddOrUpdate(object sender, EventArgs e)
    {
        if (validationManager.Validate())
        {
            user.FirstName = tbFirstName.Text;
            user.LastName = tbLastName.Text;
            userService.UpdateUser(user);
            Response.Redirect(string.Format("{0}", redirectUrl));
        }
    }

    #endregion

    #region private functions

    private void GetParams()
    {
    }

    private void LoadData()
    {
        try
        {
            user = userService.GetUserById(WebSession.CurrentUser.UserId);
        }
        catch
        {
            user = null;
        }
        roles = roleService.GetRoles();
    }

    private void UpdateView()
    {
        AddRoles();
        (Master as Admin_MasterPage).SelectedTab = "userManagement";
        pageTitle.Text = Resources.Strings.UpdateMyInfo;
        hlChangePassword.Enabled = true;
        btAddOrUpdate.Text = Resources.Strings.Update;
        roleList.SelectedValue = user.Roles[0].RoleId.ToString();

        tbUsername.Text = user.Username;
        tbEmail.Text = user.Email;
        tbFirstName.Text = user.FirstName;
        tbLastName.Text = user.LastName;
        tbUsername.ReadOnly = tbEmail.ReadOnly = true;

  //      if (WebSession.CurrentUser.IsInRole(RoleType.User))
            roleList.Enabled = false;

        hlChangePassword.NavigateUrl = string.Format("{0}userId={1}", hlChangePassword.NavigateUrl, user != null ? user.UserId : 0);
    }

    private void AddRoles()
    {
        roleList.DataSource = roles;
        roleList.DataTextField = "Name";
        roleList.DataValueField = "RoleId";
        roleList.DataBind();

        roleList.SelectedIndex = 0;
    }

    private void AddRules()
    {
        validationManager.AddRule(new ValueRequiredRule(tbFirstName, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new ValueRequiredRule(tbLastName, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));

        validationManager.Notifier = new BalloonNotifier();
    }

    #endregion

    #region public functions



    #endregion
}
